﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace BusinessDll
{
    public class product_custom
    {
        public static Semaphore semaphore;
        public product_custom() {
            // 创建一个初始计数值为3的Semaphore实例  
            semaphore = new Semaphore(0, 10);
        }

        //测试函数
        static void RunThread(object state)
        {
            
            // 获取当前线程编号  
            int threadId = (int)state;

            // 尝试获取一个许可，如果计数器为0，则该操作会阻塞，直到计数器大于0  
            Console.WriteLine("Thread {0} waiting for semaphore", threadId);
            semaphore.WaitOne();
            Console.WriteLine("Thread {0} acquired semaphore", threadId);

            // 模拟一些工作  
            try
            {
                Console.WriteLine("Thread {0} doing work", threadId);
                Thread.Sleep(1000); // 等待1秒  
                Console.WriteLine("Thread {0} finished work", threadId);
            }
            finally
            {
                // 释放一个许可，如果计数器的值大于1，则该操作会阻塞，直到计数器的值小于1  
                semaphore.Release();
                Console.WriteLine("Thread {0} released semaphore", threadId);
            }
        }
    }
}
